/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.slice;

import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.media.image.PixelMap;
import ohos.media.image.common.Position;

import com.cv4j.app.ResourceTable;
import com.cv4j.app.app.BaseAbilitySlice;
import com.cv4j.app.utill.PixelMapUtil;
import com.cv4j.core.binary.ConnectedAreaLabel;
import com.cv4j.core.binary.Erode;
import com.cv4j.core.binary.Threshold;
import com.cv4j.core.datamodel.ByteProcessor;
import com.cv4j.core.datamodel.CV4JImage;
import com.cv4j.core.datamodel.Size;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Random;

/**
 * 硬币计数
 *
 * @since 2021-05-17
 */
public class CoinsAbilitySlice extends BaseAbilitySlice {
    private Image image0;
    private Image image1;
    private Image image2;
    private Image image3;
    private Text numTextView;

    @Override
    protected int getUIContentLayoutId() {
        return ResourceTable.Layout_ability_coins;
    }

    @Override
    protected void initComponents(Intent intent) {
        image0 = (Image) findComponentById(ResourceTable.Id_image0);
        image1 = (Image) findComponentById(ResourceTable.Id_image1);
        image2 = (Image) findComponentById(ResourceTable.Id_image2);
        image3 = (Image) findComponentById(ResourceTable.Id_image3);
        numTextView = (Text) findComponentById(ResourceTable.Id_numTextView);

        PixelMap pixelMap = PixelMapUtil.getPixelMapFromResource(getContext(), ResourceTable.Media_test_coins).get();
        image0.setPixelMap(pixelMap);

        CV4JImage cv4jimage = new CV4JImage(pixelMap);
        Threshold threshold = new Threshold();
        final int color = 255;
        threshold.process((ByteProcessor) cv4jimage.convert2Gray().getProcessor(),
            Threshold.THRESH_OTSU, Threshold.METHOD_THRESH_BINARY_INV, color);
        image1.setPixelMap(cv4jimage.getProcessor().getImage().toBitmap());

        Erode erode = new Erode();
        cv4jimage.resetBitmap();
        final int three = 3;
        final int ten = 10;
        erode.process((ByteProcessor) cv4jimage.getProcessor(), new Size(three), ten);
        image2.setPixelMap(cv4jimage.getProcessor().getImage().toBitmap());

        ConnectedAreaLabel connectedAreaLabel = new ConnectedAreaLabel();
        int[] mask = new int[cv4jimage.getProcessor().getWidth() * cv4jimage.getProcessor().getHeight()];
        final int num = connectedAreaLabel.process((ByteProcessor) cv4jimage.getProcessor(), mask, null, false);
        Map<Integer, Integer> colors = new HashMap<>();
        Random random = new Random();
        int height = cv4jimage.getProcessor().getHeight();
        int width = cv4jimage.getProcessor().getWidth();
        int size = height * width;
        for (int count = 0; count < size; count++) {
            int cols = mask[count];
            if (cols >= 0) {
                colors.put(cols, Color.argb(color, random.nextInt(color),
                    random.nextInt(color), random.nextInt(color)));
            }
        }
        cv4jimage.resetBitmap();
        PixelMap newBitmap = cv4jimage.getProcessor().getImage().toBitmap();
        for (int row = 0; row < height; row++) {
            for (int col = 0; col < width; col++) {
                int cols = mask[row * width + col];
                if (cols >= 0) {
                    newBitmap.writePixel(new Position(col, row), colors.get(cols));
                }
            }
        }
        image3.setPixelMap(newBitmap);
        if (num > 0) {
            numTextView.setText(String.format(Locale.ROOT, "总计识别出%d个硬币", num));
        }
    }
}
